<template>
    <div class="login_container">
        <div class="head">
            <img src="../assets/head.png" width="1500" />
        </div>
        <div class="headered">
            <div>
                <div class="title">
                    <span style="font-size: 38px">登录</span>
                </div>
            </div>
            <div style="display: flex; flex-direction:  ">
                <div class="photo">
                    <img
                        style="
                            width: 384px;
                            height: 489px;
                            background-color: #eee;
                        "
                        src="../assets/2233.png"
                    />
                </div>
                <div class="body"></div>
                <div class="login_box">
                    <!-- 头像区 -->
                    <div class="avatar_box">
                        <img
                            style="
                                width: 100%;
                                height: 100%;
                                background-color: white;
                                border-radius: 50%;
                            "
                            src="../assets/bilibili_logo.png"
                            alt=""
                        />
                    </div>
                    <!-- 登录表单区 -->
                    <el-form
                        ref="loginFormRef"
                        status-icon
                        :model="loginForm"
                        :rules="loginFormRules"
                        class="login_form"
                    >
                        <!-- 用户名 -->
                        <el-form-item prop="userid">
                            <el-input
                                v-model="loginForm.userid"
                                prefix-icon="el-icon-user"
                                class="login_input"
                            ></el-input>
                        </el-form-item>
                        <!-- 密码 -->
                        <el-form-item prop="password">
                            <el-input
                                v-model="loginForm.password"
                                prefix-icon="el-icon-lock"
                                class="login_input"
                                type="password"
                            ></el-input>
                        </el-form-item>
                        <!-- 按钮区域 -->
                        <el-form-item class="btns">
                            <el-button type="primary" @click="login"
                                >登录</el-button
                            >
                            <el-button type="success" @click="sign"
                                >注册</el-button
                            >
                            <el-button type="warning" @click="resetLoginForm"
                                >重置</el-button
                            >
                            <el-button type="danger" @click="forget"
                                >忘记密码</el-button
                            >
                        </el-form-item>
                    </el-form>
                </div>
            </div>
        </div>
        <div class="foot"></div>
    </div>
</template>

<script>
import Navigation from "@/components/Navigation.vue";
export default {
    components: {
        Navigation: Navigation,
    },
    data() {
        return {
            //登录表单数据绑定区域
            loginForm: {
                userid: "",
                password: "",
            },
            //表单的验证规则对象
            loginFormRules: {
                //验证用户名是否合法
                userid: [
                    {
                        required: true,
                        message: "请输入用户名",
                        trigger: "blur",
                    },
                    {
                        min: 3,
                        max: 10,
                        message: "长度在 3 到 10 个字符",
                        trigger: "blur",
                    },
                ],
                //验证密码是否合法
                password: [
                    { required: true, message: "请输入密码", trigger: "blur" },
                    {
                        min: 6,
                        max: 15,
                        message: "长度在 6 到 15 个字符",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    methods: {
        //点击重置按钮  重置登录表单
        resetLoginForm() {
            // console.log(this);
            this.$refs.loginFormRef.resetFields();
            this.$message({ message: "表单已重置！", type: "warning" });
        },
        //登录预验证
        login() {
            this.$refs.loginFormRef.validate((valid) => {
                console.log(valid);
                this.$api.__api_list_users_get().then((data) => {
                    let u = data.length;
                    for (let i = 0; i < data.length; i++) {
                        u--;
                        if (
                            data[i].userid == this.loginForm.userid &&
                            data[i].password == this.loginForm.password
                        ) {
                            window.sessionStorage.setItem(
                                "token",
                                data[i].token
                            );
                            this.$message.success("bilibili，乾杯！");
                            /* 
                将登录成功之后的 token 保存到客户端的 sessionStorage 中
                    项目中除了登录之外的其他API接口 必须在登录之后才能访问
                    token 只应在当前网站打开期间生效 所以将 token 保存在 sessionStorage 中
                通过编程式导航跳转到后台主页 路由地址是 /home
                若 token 值为 123123  则该账户为超级管理员账户  页面立刻跳转至后台管理页面
              */

                            if (data[i].token == "123123") {
                                this.$router.push("/videoabout");

                                //console.log(123123);
                            } else {
                                this.$router.push("/home");
                            }
                            i = data.length;
                        }
                    }
                    if (u == -1) {
                        this.$message.error("用户名或密码错误！");
                    }
                });
            });
        },
        //忘记密码
        forget() {
            this.$message.error("建议您再去注册一个账号呢^_^");
        },
        //注册
        sign() {
            this.$router.push("/sign");
        },
    },
};
</script>

<style scoped>
.headered {
    margin: auto;
    display: flex;
    flex-direction: column;
    padding-top: 50px;
    width: 1180px;
    left: 0;
    right: 0;
    height: 650px;
}
.login_container {
    width: 1500px;
    margin: auto;
}
.photo {
    width: 384px;
    height: 489px;
    position: relative;
    transform: translate(15%, 5%);
}
.login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: relative;
    transform: translate(20%, 45%);
    border: solid;
}
.avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 7px;
    box-shadow: 0 0 10px #00b2fd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #00b2fd;
}
.btns {
    width: 350px;
    display: flex;
    justify-content: flex-end;
    margin-bottom: -150px;
}
.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 170px 50px;
    box-sizing: border-box;
}
.login_input {
    margin-bottom: 5px;

    width: 100%;
}

.head img {
    height: 10%;
    width: 100%;
    text-align: center;
}
.body {
    border-right: 2px solid rgb(17, 15, 15);
    height: 550px;
    margin-top: 28px;
    position: relative;
    padding-left: 206px;
}
.foot {
    height: 20%;
    width: 100%;
    background-color: #f6f9fa;
}
.title {
    /* padding-top: 2.5%; */
    text-align: center;
    border-bottom: 2px solid rgb(17, 15, 15);
    height: 40px;
}
.title span {
    height: 56px;
    line-height: 76px;
    margin: 0 auto;
    padding: 0 20px;
    font-size: 40px;
    background: #fff;
    text-align: center;
}
</style>